package game;

import java.awt.Point;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class LevelParser {

	private File levelFile;
	private BufferedReader inputLevel;
	private Board boardParsed;

	public LevelParser(String levelPath) {
		levelFile = new File(levelPath);
		try {
			inputLevel = new BufferedReader(new FileReader(levelFile));
			parseLevel();
		} catch (FileNotFoundException e1) {
			System.out.println("Error1");
		} catch (IOException e2) {
			System.out.println("Error2");
		}

	}

	private void parseLevel() throws IOException {
		boolean blue = false;
		String line;
		String[] arrayString;
		PieceBlue pieceBlue = null;
		List<Piece> pieces= new ArrayList<Piece>();
		while ((line = inputLevel.readLine()) != null) {
			line = line.replace(" ", "").replace("\t", "").replace("\n", "")
					.split("#")[0];
			arrayString = line.split(",");
			if (arrayString.length != 5) {
				throw new IOException();
			} else {
				if (Integer.valueOf(arrayString[0]) == -1) {
					pieceBlue = new PieceBlue(-1, new Point(Integer
							.valueOf(arrayString[1]), Integer
							.valueOf(arrayString[2])), Integer
							.valueOf(arrayString[3]), Boolean
							.valueOf(arrayString[4]));
					blue = true;
				} else{
					pieces.add(new Piece(Integer
							.valueOf(arrayString[0]), new Point(Integer
							.valueOf(arrayString[1]), Integer
							.valueOf(arrayString[2])), Integer
							.valueOf(arrayString[3]), Boolean
							.valueOf(arrayString[4])));
				}
			}
		}
		if(blue = false){
			throw new IOException();
		}
		boardParsed = new Board(6,6,new Point(5, 2),pieceBlue, pieces);
	}

	public Board getBoardParsed() {
		return boardParsed;
	}
}
